
window.addEventListener('load'  , function()  { Tabsontop.OnLoad() }, false);
//xxwindow.addEventListener('resize', function(e) { Tabsontop.OnTabBarResize(e) }, false);

var Tabsontop = {
	
	myPlaceBar: null,
	isMultibar: true,

	originalPlaceBarHeight: -1,
	
    OnLoad : function() {
		try{
			document.getElementById('main-window').setAttribute('nav_platform', navigator.platform );
		}
		catch(ex){ this.debuglog("error: "+ex, ex); }
		
		try{
			this.myPlaceBar= document.getElementById('tabsontop-place-bar');
			
			this.originalPlaceBarHeight= this.myPlaceBar.boxObject.height - 1; //  -1 !
					//this.myPlaceBar.getAttribute("height");
			
			Tabsontop.SetMultibar();
			setTimeout(	function(){ Tabsontop.CleanMultibar(); }, 4000 ); // clean things if there isnt multibar-like-addons
		}
		catch(ex){ this.debuglog("error: "+ex, ex); }
    },
	
	anonFuncMultibar: null,
	
    SetMultibar : function() {
				Tabsontop.isMultibar = true;
				
				this.anonFuncMultibar= function(e) { Tabsontop.OnTabBarResize(e) };
				gBrowser.mTabContainer.addEventListener   ("DOMAttrModified", this.anonFuncMultibar, false); 
	},
	
    CleanMultibar : function() { //  CLEAN MULTIBAR  SETTINGS !!!
	
		if( Tabsontop.isMultibar == true ){

			var attr_flowing = gBrowser.mTabContainer.getAttribute("flowing" );
			var attr_multibar= gBrowser.mTabContainer.getAttribute("multibar");
		
			/*
			this.myDumpToConsole("Tabs On top:vars for cleaning: "+
				attr_flowing +"   "+
				attr_multibar+
				"   " );
			*/
			if(  attr_flowing  != "multibar" &&
				(attr_multibar == null || (""+attr_multibar).length == 0) ){
				
				gBrowser.mTabContainer.removeEventListener("DOMAttrModified", this.anonFuncMultibar, false); 
				
				if( this.originalPlaceBarHeight < 23 ){
					this.originalPlaceBarHeight = 23;
				}
				this.myPlaceBar.setAttribute("height", ""+this.originalPlaceBarHeight );
				//this.myPlaceBar.height= this.originalPlaceBarHeight;
				Tabsontop.isMultibar= false;
				
				// this.myDumpToConsole("Tabs On top: CLEANING  OnTabBarResize (for Multibar).");
				
			}else{ // CONFIRMED is Multibar !!!
				this.myPlaceBar.setAttribute("toolbarname", this.myPlaceBar.getAttribute("toolbarname")+" (multirow mode)" );
			}
		}
	},
	
	OnTabBarResize : function(event) {
		
		if( Tabsontop.isMultibar == false){
			this.myDumpToConsole("SKIPPING  OnTabBarResize!!! (shouldnt stumble upon this!! (bad removeListener ??)");
			return;
		}
		
		if( event &&
			event.target.getAttribute("anonid") == "tabcontainer" &&
			event.attrName == "style"
		  ){ // first-tab  last-tab   collapse -> for height=0
			
			var newH= gBrowser.mTabContainer.boxObject.height;
			newH-=4;
			if( newH < 24 ){
				newH = 24;
			}	
			//this.myPlaceBar.style.height= newH+"px";
			//myPlaceBar.setAttribute("style", "height:"+newH+"px; max-height:"+newH+"px;"); 
			this.myPlaceBar.setAttribute("height", ""+newH ); 
		}
	},
	
    TabsPlaceBar_onDOMAttrModified : function(event) {
		try
		{
			if( event.target.id != 'tabsontop-place-bar' ||	
				event.attrName  != 'collapsed' ){
				return;
			}				
			document.getElementById('main-window').setAttribute('tabsontop-bar-collapsed', (event.newValue == 'true') );
	
		}
		catch(ex){ this.debuglog("error: "+ex, ex); };	
	},
	
	debuglog : function(aMessage, theException){
		this.myDumpToConsole(aMessage + (theException? "  \n\r"+theException.stack: "") );
	},
	
	myDumpToConsole : function(aMessage){
		var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
		consoleService.logStringMessage("" + aMessage);
	},
    
};
